<template>
	<view class="tender-sort">
		<jhj-sort ref="sort" class="sort" :sortData="sortData" @click="sortClick"></jhj-sort>
		<view class="content">
			<you-scroll ref="scroll" :fetch="getTenderData">
				<view slot-scope="{row,index}" @click="goDetailPage(row,index)">
					<tender-list-item :item="row"></tender-list-item>
				</view>
			</you-scroll>
		</view>
	</view>
</template>

<script>
	import TenderListItem from './components/TenderListItem.vue'
	import youScroll from '@/components/uni-list-scroll/you-scroll'
	import JhjSort from '@/components/jhj-sort/jhj-sort.vue'
	import {
		getTenderList
	} from '@/static/js/api/tender.js';
	var self;
	export default {
		components: {
			JhjSort,
			youScroll,
			TenderListItem
		},
		data() {
			return {
				sortData: [
					[{
						name: '全部',
						isCheck: true
					}, {
						name: '今天',
						isCheck: false
					}, {
						name: '近三天',
						isCheck: false
					}, {
						name: '近七天',
						isCheck: false
					}],
					[{
						name: '全部',
						isCheck: true
					}, {
						name: '资格审查',
						isCheck: false
					}, {
						name: '开标评审',
						isCheck: false
					}]
				],
				state: '',
				dateTime: []
			}
		},
		onLoad(option) {
			this.state = option.state=='全部分类'?'': option.state;
		},
		mounted() {
			self = this;
			let i1 = 1;
			let i2 = 0;
			this.sortData[1].forEach((item2, index2) => {
				if (this.state == item2.name) {
					i2 = index2
				} else {
					item2.isCheck = false;
				}
			})

			this.$refs.sort.changeData(1, i2)
			this.$refs.scroll.loadData();
		},
		methods: {
			getTenderData(pageIndex, pageSize) {
				let userInfo = JSON.parse(uni.getStorageSync('userInfo'));
				// let domain = `[('user_id','=',${userInfo.uid})]`
				let domain = `[('user_id','=',${userInfo.uid}),`;
				if (self.state) {
					domain += `('state','=','${self.state}'),`
				}
				if (self.dateTime.length > 0) {
					domain += `('create_date','>=','${self.dateTime[1]}'),('create_date','<','${self.dateTime[0]}')`
				}
				domain += ']'
				return getTenderList(pageIndex, pageSize, domain).then(res => {
					return res.data;
				})
			},
			goDetailPage(item, index) {
				uni.navigateTo({
					url: `/pages/home/tender/TenderDetailPage?id=${item.id}`
				})
			},
			sortClick(item, index, index2) {
				if (index == 0) {
					if (index2 == 1) {
						this.dateTime = [this.getDay(0), this.getDay(0)]
					} else if (index2 == 2) {
						this.dateTime = [this.getDay(0), this.getDay(-3)]
					} else if (index2 == 3) {
						this.dateTime = [this.getDay(0), this.getDay(-7)]
					} else {
						this.dateTime = [];
					}
				} else {
					if (item.name == '全部') {
						this.state = ''
					} else {
						this.state = item.name
					}
				}
				this.$refs.scroll.loadData();

			},
			getDay(day) {
				var today = new Date();
				var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
				today.setTime(targetday_milliseconds); //注意，这行是关键代码
				var tYear = today.getFullYear();
				var tMonth = today.getMonth();
				var tDate = today.getDate();
				tMonth = this.doHandleMonth(tMonth + 1);
				tDate = this.doHandleMonth(tDate);
				return tYear + "-" + tMonth + "-" + tDate;
			},
			doHandleMonth(month) {
				var m = month;
				if (month.toString().length == 1) {
					m = "0" + month;
				}
				return m;
			}
		}
	}
</script>

<style lang="less" scoped>
	.tender-sort {
		height: 100vh;
		position: relative;
		overflow: hidden;
	}

	.sort {
		position: absolute;
		top: 0;
		right: 0;
		left: 0;
	}

	.content {
		height: calc(100% - 280rpx);
		margin-top: 280rpx;
		background: #F4F5F9;
	}
</style>
